package com.top.myapplication

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.AppBarLayout

class ScrollingActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scrolling)
//        setSupportActionBar(findViewById(R.id.toolbar))
//        findViewById<CollapsingToolbarLayout>(R.id.toolbar_layout).title = title
//        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
//            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                    .setAction("Action", null).show()
//        }

        findViewById<AppBarLayout>(R.id.app_bar).apply {
            offsetTopAndBottom(200)
            addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
                override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
                    Log.e("TAG","====================$verticalOffset")
                }

            })
        }
        findViewById<Toolbar>(R.id.toolbar).apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                setStateListAnimator(null)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                elevation = 0f
            }
        }

        val adapter1 = HeadsLabelAdapter(this@ScrollingActivity)
        with(findViewById<RecyclerView>(R.id.recyclerView)) {
            layoutManager = LinearLayoutManager(this@ScrollingActivity)
            adapter = adapter1
        }

        adapter1.addData()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_scrolling, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }

    class HeadsLabelAdapter(private val mContext: Context) :
        RecyclerView.Adapter<RecyclerViewHolder>() {

        private var count = 0

        fun addData() {
            count = 20
            notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder =
            RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_test, parent, (false))
            )

        override fun getItemCount(): Int = count

        override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
            Log.e("TAG", "$position")
        }
    }

    class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}